/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seso;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;

/**
 *
 * @author Administrator
 */
public class RComboBox extends javax.swing.JComboBox {

    RComboBoxBorder border = new RComboBoxBorder();
    RComboBoxButton button = new RComboBoxButton();

    private boolean focus_Gained = false, mouse_Entered = false;
    private boolean isFirstTime =  true;
  

    public RComboBox() {
        super();
        initControl();
    }
    
    
    private void initControl()  {
        setFont(new Font("Arial Unicode MS", Font.PLAIN, 12));
        setBorder(null);
        setBackground(new Color(245,245,245));
        border.setForeground(getBackground());  // 使左边弧形区内颜色与ComboBox背景相同
        
        addListener(); 
    }
    
    private void addListener()    {

        addFocusListener(new java.awt.event.FocusAdapter() {

            public void focusGained(java.awt.event.FocusEvent evt) {
                focus_Gained = true;
                repaint();
            }

            public void focusLost(java.awt.event.FocusEvent evt) {
                focus_Gained = false;
                repaint();
            }
        });

        addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouse_Entered = true;
                repaint();
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouse_Entered = false;
                repaint();
            }
        });
        
    }

    

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        int height = getHeight();
        int width = getWidth();
        int X = getX(), Y = getY();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, width - height - 1, height, 0, 0);
        
        g2d.clip(rect);

        super.paintComponent(g);
        
        if (isFirstTime)       
        {
            isFirstTime = false;

            getParent().add(button);
            button.setBounds(X + width - height - 9, Y, height + 10, height);

            getParent().add(border);
            border.setBounds(X - 6, Y - 4, width + 11, height + 8);
        }
        
        border.doRePaint(focus_Gained); 
        button.doRePaint(mouse_Entered); 
    }

}
